package com.verlet.springbootdemo.simple.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 生产者
 *
 * @author verlet
 * @date 2020/6/27
 */
@Slf4j
@Component
public class Producer {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 简单方式
     */
    public void sendEmail() {
        String message = "send email to user";
        rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.email", message);
    }

    /**
     * 使用 org.springframework.amqp.core.Message 发送消息
     */
    public void send2() {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.getHeaders().put("attr", "新增属性");
        messageProperties.getHeaders().put("type", "消息类型");
        messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        /**
         * messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
         * messageProperties.getHeaders().put("__TypeId__", "messageDelegate");
         */
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        Message message = MessageBuilder.withBody("message消息。。。".getBytes()).andProperties(messageProperties).build();
        rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.email", message,
                msg -> {
                    msg.getMessageProperties().getHeaders().put("features", "新增消息额外处理");
                    return msg;
                });
    }

    // 确认模式 消息确认投递到服务端
    public final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            log.info("confirmCallback=> correlationData={}", correlationData);
            log.info("confirmCallback=> ack={}; cause={}", ack, cause);
        }
    };

    // 返回模式 投递消息无法路由到队列上
    public final RabbitTemplate.ReturnsCallback returnCallback = new RabbitTemplate.ReturnsCallback() {
        @Override
        public void returnedMessage(ReturnedMessage returned) {
            log.info("returnCallback=> message={}", returned.getMessage());
            log.info("returnCallback=> replyCode={}; replyText={}", returned.getReplyCode(), returned.getReplyText());
            log.info("returnCallback=> exchange={}; routingKey={}", returned.getExchange(), returned.getRoutingKey());
        }
    };

    public void send3() {
        String message = "send sms to user";
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.sms", message,
                new CorrelationData(String.valueOf(System.currentTimeMillis())));
    }

    public void send4() {
        String message = "send email to user";
        rabbitTemplate.setReturnsCallback(returnCallback);
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, "inform.email0", message,
                new CorrelationData(String.valueOf(System.currentTimeMillis())));
    }
}
